Metal: FP8-packed compressed-KV cache + long-context memory optimizations#418
Open
aledesogusbusiness-hue wants to merge 1 commit into
Open
Metal: FP8-packed compressed-KV cache + long-context memory optimizations#418aledesogusbusiness-hue wants to merge 1 commit into
aledesogusbusiness-hue wants to merge 1 commit into
Conversation
…ions Three memory optimizations for the Metal backend's compressed-KV (MLA latent) path: 1. Packed FP8 comp cache (opt-in, DS4_METAL_FP8_KV_STORE=1): stores comp rows as e4m3 + ue8m0 scale + f16 rot = 584 B/row vs 1024 B/row f16. Dequant uses a 128-entry LUT (ds4_e4m3_lut) avoiding branch+exp2. Validated bit-identical. 2. comp_mask stored as f16 (always on): binary -inf/0 mask fits exactly in f16, halving the mask buffer size at all context lengths. 3. indexer_scores token-tiling (always on): DS4_INDEXER_SCORE_TILE=512 reduces the score working buffer from comp_cap*prefill_cap to comp_cap*512 (~8x). Together: ~2.3x KV cache reduction at long context, bit-identical output, speed-neutral decode (bandwidth saving offset by per-element dequant cost). Revert with DS4_DISABLE_KV_OPTS=1. Tested on M5 Max, 8k-96k context. Fixes: antirez#416 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Contributor
|
This is the same as #416 ? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Three memory optimizations for the Metal backend's compressed-KV (MLA latent) path that together cut context-dependent KV memory by ~2.3x with bit-identical output and speed-neutral decode throughput.
DS4_METAL_FP8_KV_STORE=1): stores comp rows as e4m3 + ue8m0 scale + f16 rot = 584 B/row vs 1024 B/row f16. Dequant uses a 128-entry LUT avoiding branch+exp2.comp_maskstored as f16 (always on): binary −∞/0 mask fits exactly in f16, halving the mask buffer at all context lengths.indexer_scorestoken-tiling (always on):DS4_INDEXER_SCORE_TILE=512reduces the score working buffer fromcomp_cap × prefill_captocomp_cap × 512(~8x reduction).Results
Decode speed: break-even (bandwidth saving offset by per-element e4m3 dequant).
Controls
DS4_DISABLE_KV_OPTS=1— reverts to pre-optimization layout for A/B comparisonDS4_METAL_FP8_KV_STORE=1— enables packed FP8 (opt-in, precision change)Test environment
Apple M5 Max · DeepSeek-V4-Flash · macOS 27.0 · Metal 4.1 · 8k–96k context
🤖 Generated with Claude Code